<template>
  <a-modal
    :title="modalTitle"
    :width="1100"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" style="max-height: 500px;overflow-y: auto;overflow-x: hidden;">
        <a-form-item label="业务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-if="modalOpera==1" v-decorator="['id', {rules: [{required: true}]}]" />
        </a-form-item>

        <a-row :gutter="6">

          <a-col :xl="{span: 11}" :lg="{span: 8}" :md="{span: 12}">
            <a-form-item label="片段别名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="用于标记片段名称便于维护和设计"
                v-decorator="['alias', {rules: [{required: true, min: 2, message: '请输入至少二个字符的规则！'}]}]"
              />
            </a-form-item>
          </a-col>

          <a-col :xl="{span: 11}" :lg="{span: 8}" :md="{span: 12}">
            <a-form-item label="片段编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="编码必须为字母切不可修改"
                v-decorator="['code', {rules: [{required: true, min: 2, message: '请输入至少二个字符的规则！'}]}]"
              />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="6">
          <a-col :xl="{span: 11}" :lg="{span: 8}" :md="{span: 12}">
            <a-form-item label="数据条数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                style="width: 100%;"
                :min="1"
                v-decorator="['size', {rules: [{required: true, message: '请输入'}],initialValue:0}]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-divider dashed />
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { save, getById, update } from '@/api/site/fragment.js'
import pick from 'lodash.pick'
export default {
  data () {
    return {
      labelCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 7
        }
      },
      wrapperCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 13
        }
      },
      modalTitle: '',
      fileItem: null,
      record: null, // 模板对象
      modalOpera: 0, // 0：新增 1：编辑
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  created () {
    const _this = this
    document.onkeydown = function (e) { // 按下回车提交
        const key = window.event.keyCode
        if (key === 13 && _this.visible) {
            _this.handleSubmit()
        }
    }
  },
  methods: {
    createFile (fileItem, record) {
      this.fileItem = fileItem
      this.record = record
      this.modal('新建页面片段', 0)
    },
    edit (record) {
      this.modal('编辑页面片段', 1)
      getById({
        id: record.id
      }).then(response => {
        const result = response.res
        this.form.setFieldsValue(pick(result, 'id', 'categoryExtendName'))
        if (result.extendFieldList) {
          this.setDataSource(JSON.parse(result.extendFieldList))
        }
      })
    },
    modal (title, opera) {
      this.modalTitle = title
      this.modalOpera = opera
      this.visible = true
      this.form.resetFields()
    },
    handleSubmit () {
      const {
        form: { validateFields }
      } = this
      validateFields((errors, values) => {
        if (!errors) {
          if (this.modalOpera === 0) {
            values.path = this.fileItem.path
            values.relativePath = this.fileItem.relativePath
            values.templateId = this.record.id
            save(values).then(response => {
              this.message(response)
            })
          } else if (this.modalOpera === 1) {
            update(values).then(response => {
              this.message(response)
            })
          }
        }
        this.confirmLoading = false
      })
    },
    message (response) {
      if (response.code === 0) {
        this.$emit('ok')
        this.handleCancel()
      } else {
        this.$message.error(response.msg)
      }
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    }
  }
}
</script>
